<%--
  Created by IntelliJ IDEA.
  User: user
  Date: 11.05.12
  Time: 18:41
  To change this template use File | Settings | File Templates.
--%>
<%@ page import="db.DBConnection" %>
<%@ page import="db.User" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="db.Account" %>
<%
    int user_id = -1;
    String secret1 = "";
    Cookie[] cookies = request.getCookies();

    for (Cookie cookie : cookies) {
        if (cookie.getName().equals("user_id")) {
            user_id = Integer.parseInt(cookie.getValue());
        } else if (cookie.getName().equals("secret")) {
            secret1 = cookie.getValue();
        }
    }
    DBConnection dbConnection = new DBConnection();
    if (!dbConnection.isCookieOk(user_id, secret1)) {
        String redirectURL = "../index.jsp";
        response.sendRedirect(redirectURL);
    }

    int num = dbConnection.doGetNumberOfUnreadMessages(user_id);

    String message = request.getParameter("message");
    String send_to = request.getParameter("send_to");
    String message_id = request.getParameter("message_id");
    
    boolean isOk = false;
    boolean hasParams = false;
    if (send_to != null && message_id != null && message != null) {
        hasParams = true;
        int receiver_id = Integer.parseInt(send_to);
        isOk = dbConnection.doReply(user_id, receiver_id, message, Integer.parseInt(message_id));
    }
%>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>See personal info</title>
    <link rel="stylesheet" type="text/css" href="style"/>
</head>

<body>
<div class="logout" style="text-align:right;"><a href="../logout.jsp">  Logout</a></div>
<div class="header"><img src="logo.jpg"></div>
<div class="layout">
    <div class="sidebar">
        <ul class="cssmenu">
            <li><a href="see_personal_info.jsp">Personal info</a></li>
            <li><a href="operations.jsp">Operations</a></li>
            <li><b><a href="messages.jsp">Messages<% if (num != 0) {%> <b> (<%=num%>)</b> <%}%></a></b></li>
        </ul>
    </div>
    <% if (hasParams) {
        if (isOk) {
    %> <p style="color: green;"> Message sent! </p> <%
} else {
%> <p style="color: red;"> <%=dbConnection.lastOperationError%> </p><%
        }
    } %>
    <form method="post" action="reply.jsp">
        <table>
            <tr>
                <td> Reply to, ID: </td>
                <td>
                    <input type="text" name="message_id" value="<%=message_id%>" readonly="readonly" >
                </td>
            </tr>
            <tr>
                <td> Send to, ID: </td>
                <td>
                    <input type="text" name="send_to" value="<%=send_to%>" readonly="readonly" >
                </td>
            </tr>
            <td> Message: </td>
            <td>
                <textarea rows="15" cols="30" name="message"></textarea>
            </td>
        </table>
        <input type="submit" value="Send message">
    </form>
</div>
<div class="content">
</div>
</body>
</html>